package drafthj.javase.concurrent.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 *
 * @author drafthj
 * @date 2021/7/20
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class ThenRunTest {
    public static void main(String[] args) throws InterruptedException {
        Executor executor = Executors.newFixedThreadPool(10);
        CompletableFuture<Void> stepOneFuture = CompletableFuture.runAsync(
                () -> {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + " step 1 start.....");
                    sleep(2);
                    System.out.println(threadName + " step 1 end.....");
                }, executor
        );
        CompletableFuture<Void> stepTwoFuture = stepOneFuture.thenRun(() -> {
                    String threadName = Thread.currentThread().getName();
                    System.out.println(threadName + " step 2 start.....");
                    sleep(2);
                    System.out.println(threadName + " step 2 end.....");
                }
        );
        System.out.println("main");
        TimeUnit.SECONDS.sleep(6);
    }

    private static void sleep(long timeout) {
        try {
            TimeUnit.SECONDS.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
